package com.briup.server;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.briup.entity.Environment;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.*;
import java.util.Collection;
import java.util.Properties;

public class test2 {
    public static void main(String[] args) throws Exception {
        Collection<Environment> receive = new ServerImpl().receive();
        int i = 0;
//        PreparedStatement ps = null;
        for (Environment environment : receive) {
            Timestamp time = environment.getTime();
            int date = time.getDate();
            Properties prop = null;
            prop.load(new FileInputStream("D:\\env_project\\server\\src\\main\\resources\\druid.properties"));
            DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
            Connection conn = dataSource.getConnection();
            String tab = "env_detail_" + date;
            String s = tab.toUpperCase();
            String sql = "insert into "+s+" values(?,?,?,?,?,?,?,?,?)";
            conn.setAutoCommit(false);
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, environment.getName());
            ps.setString(2, environment.getSrcId());
            ps.setString(3, environment.getDesId());
            ps.setString(4, environment.getAddress());
            ps.setInt(5, environment.getCount());
            ps.setString(6, environment.getCmd());
            ps.setInt(7, environment.getStatus());
            ps.setFloat(8, environment.getData());
            ps.setTimestamp(9, environment.getTime());
            ps.addBatch();

            i++;

                ps.executeBatch();

                conn.commit();
                conn.setAutoCommit(true);
                conn.close();
                ps.close();

        }
    }
}